package cn.anquing.study.qlexpress.demo;

import cn.anquing.study.qlexpress.beans.JoinOperator;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;

/**
 * 如何使用Operator
 * @author wangquanqing
 * @since 2021/2/23 10:39
 */
public class Demo3 {

    public static void main(String[] args) throws Exception {
        //(1)addOperator
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        runner.addOperator("join",new JoinOperator());
        Object r1 = runner.execute("1 join 2 join 3", context, null, false, false);
        System.out.println(r1);
        //返回结果  [1, 2, 3]

        //(2)replaceOperator
        ExpressRunner runner2 = new ExpressRunner();
        DefaultContext<String, Object> context2 = new DefaultContext<String, Object>();
        runner2.replaceOperator("+",new JoinOperator());
        Object r2 = runner2.execute("1 + 2 + 3", context2, null, false, false);
        System.out.println(r2);
        //返回结果  [1, 2, 3]

        //(3)addFunction
        ExpressRunner runner3 = new ExpressRunner();
        DefaultContext<String, Object> context3 = new DefaultContext<>();
        runner3.addFunction("join",new JoinOperator());
        Object r3 = runner3.execute("join(1,2,3,4)", context3, null, false, true);
        System.out.println(r3);
        //返回结果  [1, 2, 3, 4]
    }
}
